vimfandomcom-20200223-history
Preview output from interpreter in new window
This will come in handy for those of you who script in an interpreted language. If you ever wanted to just run parts of your script and check the output without manipulating your code, read on. The output of your whole script or your snippets will be shown in the "preview window" ( ). In my case I used Ruby and mapped the commands to F7. I just select the stuff I want to run and press F7. The output is shown in a new window! The next time I press F7 the window will be closed and replaced. "save code, run ruby, show output in preview window function! Ruby_eval_vsplit() range let src = tempname() let dst = tempname() execute ": " . a:firstline . "," . a:lastline . "w " . src execute ":silent ! ruby " . src . " > " . dst . " 2>&1 " execute ":pclose!" execute ":redraw!" execute ":vsplit" execute "normal \l" execute ":e! " . dst execute ":set pvw" execute "normal \h" endfunction vmap :call Ruby_eval_vsplit() nmap mzggVG`z imap a map l:bw imap a Comments Here is a horizontal version. In this example the windows aren't recreated, which means the dimensions stay the same for the current tab. function! Ruby_eval_vsplit() range let src = tempname() let dst = tempname() execute ": " . a:firstline . "," . a:lastline . "w " . src execute ":silent ! ruby " . src . " > " . dst . " 2>&1 " execute ":pedit! " . dst endfunction Maybe you like to consider using: :w !ruby >myoutput :.r !ruby myscript.rb :!ruby Here is a generic Python version of the tip. It does not require to have Vim with Python support compiled in. "Run in the Python interpreter function! Python_Eval_VSplit() range let src = tempname() let dst = tempname() execute ": " . a:firstline . "," . a:lastline . "w " . src execute ":!python " . src . " > " . dst execute ":pedit! " . dst endfunction au BufNewFile,BufRead *.py vmap :call Python_Eval_VSplit() ---- You can create a more generic version by parsing the first line of the file. If the first line is of the form: #!/path/to/executable Then, you can use /path/to/executable to execute the selected code. If the first line of the file doesn't match, you can consider the file's extension and choose an executable based on that: file.pl - execute with perl file.sh - execute with sh file.vim - execute with vim etc I changed it a little more and added some comments. The biggest change is, that no shell window is opened (in windows). I hated the cmd window to popup, even if it's just for a second. function! Ruby_eval_vsplit() range let src = tempname() let dst = "Ruby Output" " put current buffer's content in a temp file silent execute ": " . a:firstline . "," . a:lastline . "w " . src " open the preview window silent execute ":pedit! " . dst " change to preview window wincmd P " set options setlocal buftype=nofile setlocal noswapfile setlocal syntax=none setlocal bufhidden=delete " replace current buffer with ruby's output silent execute ":%! ruby " . src . " 2>&1 " " change back to the source buffer wincmd p endfunction vmap :call Ruby_eval_vsplit() nmap mzggVG`z imap a map l:bw imap a Also see this Ruby Runner plugin: http://henrik.nyh.se/2011/03/vim-ruby-runner